import JSZip from 'jszip'

export interface ParsedWayline {
  geojson: GeoJSON.FeatureCollection
  rawKml: string
}

export async function parseKmzFile(file: File): Promise<ParsedWayline> {
  const zip = await JSZip.loadAsync(file)
  const kmlEntry = Object.values(zip.files).find(f => f.name.toLowerCase().endsWith('.kml'))
  if (!kmlEntry) throw new Error('KMZ 内未找到 KML 文件')
  const kmlText = await kmlEntry.async('text')

  toGeoJSON(kmlText).then((from:any) => {
    console.log(from)
  })
  // const xml = new DOMParser().parseFromString(kmlText, 'text/xml')
  // const geojson = kml(xml) as GeoJSON.FeatureCollection
  
  return { geojson, rawKml: kmlText }
}


